package com.springboot.controller;

import com.springboot.dto.AssistantSeePatientView;
import com.springboot.model.OrderForm;
import com.springboot.model.PatientInfoForm;
import com.springboot.model.UserLoginForm;
import com.springboot.service.AssistantSeePatientListService;
import com.springboot.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Controller
public class AssistantSeePatientList  {
    @Autowired
    UserLoginService userLoginService;
    @Autowired
    AssistantSeePatientListService assistantSeePatientListService;

    /**
     * 助理查看医生列表
     */
    @CrossOrigin
    @RequestMapping(value = "/getPatientSerlvet123")
    public @ResponseBody
    String assistantSeePatient(@RequestParam("chooseFlag") Boolean flag,
                               @RequestParam("chooseDate") String chooseDate,
                               @RequestParam("doctorTel")  String tel)
    {
        System.out.println("助理正在查看医生+tel+的患者预约列表");
        try{
            UserLoginForm userLoginForm=userLoginService.findUserByTel(tel);
            Integer doctorId=userLoginForm.getUserId();
            ArrayList<AssistantSeePatientView> assistantSeePatientViews=new ArrayList<>();

            //表示助理未选择日期，默认日期为近期
            if(!flag)
            {
                System.out.println("查看近期");
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                Date now = new Date();
                String dateTemp = dateFormat.format( now );//今日日期
                ArrayList<OrderForm> orderForms=new ArrayList<>();
                orderForms=assistantSeePatientListService.getRecentlyOrderListByDoctorTel(doctorId);
                int size=orderForms.size();
                if(size==0) return "该医生"+dateTemp+"无预约";
                System.out.println(size);
                for(int i=0;i<size;i++){
                    String dateTemp1 = dateFormat.format( orderForms.get(i).getBootDate() );
                    int temp=dateTemp.compareTo(dateTemp1);
                    System.out.println(temp);
                    if(temp>0) {
                        continue;
                    }
                    int bootId=orderForms.get(i).getBootId();
                    int patientId=orderForms.get(i).getPatientId();
                    Date bootDate1=orderForms.get(i).getBootDate();
                    String bootDate=dateFormat.format(bootDate1);
                    String orderTime=orderForms.get(i).getBootTime();
                    PatientInfoForm patientInfoForm=new PatientInfoForm();
                    patientInfoForm=assistantSeePatientListService.getPatientInfoFormByBootId(bootId);
                    String sickName=patientInfoForm.getSickSketch();
                    UserLoginForm userLoginForm1=new UserLoginForm();
                    userLoginForm1=userLoginService.findUserById(patientId);
                    String logoLink=userLoginForm1.getLogoLink();
                    String patientName=userLoginForm1.getUserName();
                    String patientTel=userLoginForm1.getUserTel();
                    AssistantSeePatientView assistantSeePatientView=new AssistantSeePatientView(logoLink,patientName,bootDate,orderTime,sickName,patientTel);
                    assistantSeePatientViews.add(assistantSeePatientView);
                }
                int appointmentNum=assistantSeePatientViews.size();
                //对预约日期进行排序
                for(int i=0;i<appointmentNum;i++)
                {
                    for(int j=0;j<appointmentNum;j++)
                    {
                        int temp1=assistantSeePatientViews.get(i).getOrderDate().compareTo(assistantSeePatientViews.get(j).getOrderDate());
                        if(temp1>0||(temp1==0&&assistantSeePatientViews.get(i).getOrderTime()=="下午")&&assistantSeePatientViews.get(j).getOrderTime()=="上午")
                        {
                            AssistantSeePatientView assistantSeePatientView=new AssistantSeePatientView();
                            assistantSeePatientView=assistantSeePatientViews.get(i);//对吗？？
                            int a=assistantSeePatientViews.get(i).setValue(assistantSeePatientViews.get(j));
                            int b=assistantSeePatientViews.get(j).setValue(assistantSeePatientView);
                        }
                    }

                }
            }
            else
            {
                System.out.println("查看选中日期");
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date date = null;
                try {
                    date = format.parse(chooseDate);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                String dateTemp = dateFormat.format( date );
                System.out.println(doctorId);
                ArrayList<OrderForm> orderForms=new ArrayList<>();
                orderForms=assistantSeePatientListService.getRecentlyOrderListByDoctorTel(doctorId);
                int size=orderForms.size();
                if(size==0) return "该医生"+dateTemp+"无预约";
                System.out.println(size+" "+doctorId);
                for(int i=0;i<size;i++){
                    String dateTemp1 = dateFormat.format( orderForms.get(i).getBootDate() );
                    if(!dateTemp1.equals(dateTemp)) {
                        continue;
                    }
                    OrderForm orderForm=new OrderForm();
                    orderForm=orderForms.get(i);
                    int bootId=orderForm.getBootId();
                    int patientId=orderForm.getPatientId();
                    Date bootDate1=orderForm.getBootDate();
                    String bootDate=dateFormat.format(bootDate1);
                    String orderTime=orderForms.get(i).getBootTime();
                    PatientInfoForm patientInfoForm=new PatientInfoForm();
                    patientInfoForm=assistantSeePatientListService.getPatientInfoFormByBootId(bootId);
                    String sickName=patientInfoForm.getSickSketch();
                    UserLoginForm userLoginForm1=new UserLoginForm();
                    userLoginForm1=userLoginService.findUserById(patientId);
                    String logoLink=userLoginForm1.getLogoLink();
                    String patientName=userLoginForm1.getUserName();
                    String patientTel=userLoginForm1.getUserTel();
                    AssistantSeePatientView assistantSeePatientView=new AssistantSeePatientView(logoLink,patientName,bootDate,orderTime,sickName,patientTel);
                    assistantSeePatientViews.add(assistantSeePatientView);
                }
            }
            return assistantSeePatientViews.toString();
        }
        catch (Exception e)
        {
            return "Flase";
        }
    }
}
